package hu.issue.framework.web.servlet;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

class ViewHandler {

	public void handleView(final String view, final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException {
		if (view.startsWith("redirect:")) {
			response.sendRedirect(view.substring(9));
		} else {
			request.setAttribute("hu.issue.framework.page", view);
			request.getRequestDispatcher("/WEB-INF/jsp/page.jsp").forward(request, response);
		}
	}

	public void handleException(final Exception ex, final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
		final List<Throwable> throwableList = new LinkedList<Throwable>();
		Throwable t = ex;
		do {
			throwableList.add(t);
		} while ((t = t.getCause()) != null);
		request.setAttribute("hu.issue.framework.causes", throwableList);
		request.getRequestDispatcher("/WEB-INF/jsp/exception.jsp").forward(request, response);
	}

}
